package com.base.service.system.controller;

import java.util.Map;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.base.core.context.annotation.AuthenticationCheck;
import com.base.core.context.annotation.RestfulCheck;
import com.base.core.context.mvc.BaseV1Controller;
import com.base.core.head.utils.DataDictionaryUtils;
import com.base.service.base.system.service.ConfigureService;
import com.gitee.magic.core.json.JsonArray;
import com.gitee.magic.core.json.JsonObject;
import com.gitee.magic.core.valid.annotation.Length;
import com.gitee.magic.core.valid.annotation.NotEmpty;
import com.gitee.magic.framework.head.vo.BaseVO;
import com.gitee.magic.framework.head.vo.ObjectVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @author start
 */
@Tag(name = "配置管理")
@RestController
public class ConfigureController extends BaseV1Controller {

    @Resource
    private ConfigureService configureService;

    @RestfulCheck
    @Operation(summary="数据字典")
	@GetMapping("/v1/configure/dictionary/{key}")
	public ObjectVO<Map<String,String>> dictionary(
			@PathVariable @Parameter(description = "字段") @Length String key) {
    	return response(DataDictionaryUtils.getDictoinary(key));
	}
    
	@RestfulCheck
	@Operation(summary="获取配置")
    @GetMapping("/v1/configure/{code}")
	public ObjectVO<Object> get(
            @PathVariable @Parameter(description = "编码") @Length String code){
    	return response(configureService.getValue(code));
    }

    @AuthenticationCheck
    @Operation(summary="更新配置")
    @PutMapping("/v1/configure/string/{code}")
    public BaseVO set(
            @PathVariable @Parameter(description = "编码") @Length String code,
    		@RequestBody @NotEmpty String value) {
		configureService.setValue(code,value);
    	return response();
	}

    @AuthenticationCheck
    @Operation(summary="更新配置")
    @PutMapping("/v1/configure/json/{code}")
    public BaseVO set(
            @PathVariable @Parameter(description = "编码") @Length String code,
    		@RequestBody @NotEmpty JsonObject value) {
		configureService.setValue(code,value);
    	return response();
	}

    @AuthenticationCheck
    @Operation(summary="更新配置")
    @PutMapping("/v1/configure/array/{code}")
    public BaseVO set(
            @PathVariable @Parameter(description = "编码") @Length String code,
    		@RequestBody @NotEmpty JsonArray value){
		configureService.setValue(code,value);
    	return response();
	}

}
